<?php

	namespace Aspic;

	class Console {
		protected $_args;
		protected $_printIfNotInConsole;

		public function __construct($argv = null, $printIfNotInConsole = false) {
			if ($argv == null) {
				$argv = $_SERVER['argv'];
			}
			
			$this->_args = $argv;
			$this->_printIfNotInConsole = $printIfNotInConsole;
		}
		
		public function getArg($index) {
			$realIndex = $index + 1;
			
			if (isset($this->_args[$realIndex])) {
				return $this->_args[$realIndex];
			}
			else {
				return null;
			}
		}
		
		public function write($str) {
			if (!defined('STDOUT') AND $this->_printIfNotInConsole) {
				print $str;
			}
			else {
				fwrite(STDOUT, $str);
			}
		}
		
		public function writeLn($str) {
			return $this->write($str."\n");
		}
		
		public function writeformat($str, $args) {
			$formated = call_user_func_array('Aspic\UString::fromArgs', func_get_args());
			
			return $this->write($formated);
		}
		
		public function read() {
			$read = fgets(STDIN);
			$read = rtrim($read, "\r\n");
			
			return $read;
		}
		
	}
	
?>